import { EjsVueCodeGenerator } from "@/core/EjsVueCodeGenerator"
import { EjsPageVueComponent } from "./BasePage"
import { EjsType } from "@/core/EjsCodeEncoder"
import { OpenApi } from "@/core/OpenApi"
import { ApplyMethodsParamItem } from "@/core/VueCodeGenerator"

class ListQueryEjsPageVueComponent extends EjsPageVueComponent {
	constructor() {
		super("./src/assets/ejs/ListPage.ejs", EjsType.LOCAL_FILE)
		this.title = '列表查询页面'
		this.apiMethods = [{
			title: '查询接口', 
			name: 'listQueryApi',
			controllerName: undefined,
			methodName: undefined,
			apiMethod: undefined,
			method: undefined
		}]
	}
}

export class ListQueryEjsPage extends EjsVueCodeGenerator {
	constructor() {
		super(new ListQueryEjsPageVueComponent())
		this.title = "列表页面模板(ejs)"
	}

	async applyMethods(openapi: OpenApi, params: ApplyMethodsParamItem[]): Promise<void> {
		function stringifyWithUndefined(obj) {
			return JSON.stringify(obj, function(_, value) {
			// 将 undefined 替换为 null
			if (value === undefined) {
				return '__undefined__';
			}
			return value;
			}, '\t').replace(/"__undefined__"/g, 'undefined');
		}
		await super.applyMethods(openapi, params, {
			parseTableParams(param: ApplyMethodsParamItem) {
				var body = param.apiMethod.bodyParam.body
				var values: any = []
				if(body) {
					var properties = body.properties
					Object.keys(properties).forEach((key) => {
						values.push(`${key}: this.searchForm.${key}`)
					})
				}
				return values
			},
			parseTableColumns(param: ApplyMethodsParamItem) {
				var response = param.apiMethod.response
				var responseBody = null
				while(response && response.definitions && response.definitions.length > 0) {
				  responseBody = response.definitions[0]
				  response = responseBody
				}
				var columns = []
				if(response) {
				  Object.keys(response.properties).map((key) => {
					var prop = response.properties[key]
					if(prop.enum) {
					  var splits = prop.description.split(',')
					  columns.push({
						field: key + '_desc',
						title: splits[0],
						width: 100
					  })
					} else {
					  columns.push({
						field: key,
						title: prop.description || key,
						width: 100
					  })
					}
				  })
				}

				return stringifyWithUndefined(columns).split('\n').join('\n\t\t\t\t')
			},
			parseSearchForm(param: ApplyMethodsParamItem) {
				var body = param.apiMethod.bodyParam.body
				var properties = body.properties
				var searchColumns: any[] = []
				var searchForm: {[key:string]: any} = {}
				Object.keys(body.properties).forEach((key) => {
					var prop = properties[key]
					searchForm[key] = undefined
					if(prop) {
						var type = 'input'
						if(prop.enum) {
							type = 'select'
							var splits = prop.description.split(',')
							var values = splits.slice(1)
							var options = values.map((value, index) => {
								var [code, name ] = value.split(':')
								return {
									code,
									name,
									label: splits[0],
									value: prop.enum[index]
								}
							})
							searchColumns.push({
								field: key,
								label: properties[key].description,
								fieldProps: {
									placeholder: '请选择'+ properties[key].description,
									options: options
								}
							})
						} else {
							searchColumns.push({
								field: key,
								label: properties[key].description,
								fieldProps: {
									placeholder: '请输入'+ properties[key].description
								}
							})
						}
						
					}
				})
				return {
					searchColumns: stringifyWithUndefined(searchColumns).split('\n').join('\n\t\t\t\t'),
					searchForm: stringifyWithUndefined(searchForm).split('\n').join('\n\t\t\t\t'),
				}
			},
		}, {
			async: true
		})
	}
}